﻿//using LuaInterface;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

namespace Nirvana
{
    /// <summary>
    /// 游戏品质参数配置表，用于控制Unity QualitySettings
    /// 说明：用于游戏运行时切换游戏运行环境，提升或降低品质，提高性能
    /// </summary>
    [CreateAssetMenu(fileName = "QualityConfig", menuName = "Nirvana/QualityConfig")]
    public class QualityConfig : ScriptableObject
    {
        private static QualityConfig instance;
#if UNITY_ANDROID || UNITY_IOS
        private static int qualityLevel = 0;//默认高0
#else
        private static int qualityLevel = 1;//默认高1
#endif
        [SerializeField]
        private QualityLevel[] levels;
        public static QualityConfig Instance
        {
            get { return instance; }
        }

        /// <summary>
        /// 品质激活触发的事件列表
        /// </summary>
        private static LinkedList<Action> onQualityChanged = new LinkedList<Action>();
        private LinkedListNode<Action> onQualityChangedNode;
        /// <summary>
        /// 添加监听事件
        /// </summary>
        /// <returns></returns>
        public static LinkedListNode<Action> ListenQualityChanged(Action action)
        {
            return onQualityChanged.AddLast(action);
        }
        /// <summary>
        /// 清除事件
        /// </summary>
        public static void UnlistenQualtiy(LinkedListNode<Action> node)
        {
            onQualityChanged.Remove(node);
        }

        public static int QualityLevel
        {
            get { return qualityLevel; }
            set
            {
                qualityLevel = value;
                foreach (var i in onQualityChanged) i.Invoke();
            }
        }

        /// <summary>
        /// 品质配置表数量
        /// </summary>
        /// <returns></returns>
        public int GetLevelCount()
        {
            return levels != null ? levels.Length : 0;
        }
        /// <summary>
        /// 获取对应等级的品质配置表
        /// </summary>
        public QualityLevel GetLevel(int index)
        {
            return levels[index];
        }
        public QualityLevel GetLevel(string name)
        {
            foreach (var level in levels)
            {
                if (level.Name == name)
                {
                    return level;
                }
            }

            return null;
        }

#if UNITY_EDITOR
        /// <summary>
        /// 查找所有品质配置表
        /// </summary>
        //[NoToLua]
        public static QualityConfig[] FindConfigs()
        {
            var configAssets = UnityEditor.AssetDatabase.FindAssets("t:QualityConfig");
            var configs = new QualityConfig[configAssets.Length];
            for (int i = 0; i < configAssets.Length; ++i)
            {
                var configAsset = configAssets[i];
                var configPath = UnityEditor.AssetDatabase.GUIDToAssetPath(configAsset);
                configs[i] = UnityEditor.AssetDatabase.LoadAssetAtPath<QualityConfig>(configPath);
            }

            return configs;
        }
#endif

        private void OnEnable()
        {
            instance = this;
            onQualityChangedNode = ListenQualityChanged(OnQualityLevelChanged);
#if !UNITY_EDITOR //避免Editor状态下频繁刷新
            OnQualityLevelChanged(); //默认激活
#endif
        }

        private void OnDisable()
        {
            if (onQualityChangedNode != null)
            {
                UnlistenQualtiy(onQualityChangedNode);
                onQualityChangedNode = null;
            }
        }

        private void OnDestroy()
        {
            if (onQualityChangedNode != null)
            {
                UnlistenQualtiy(onQualityChangedNode);
                onQualityChangedNode = null;
            }
            Assert.IsNotNull(instance);
            instance = null;
        }

        private void OnQualityLevelChanged()
        {
            if (levels == null) return;
            var level = QualityConfig.QualityLevel;
            if (level < levels.Length)
            {
                levels[level].Active();
            }
        }

    }

}
